import React from 'react'
import PropTypes from 'prop-types'
import {Avatar, Table} from 'antd'
import config from 'config'
import classnames from 'classnames'
import AnimTableBody from 'components/DataTable/AnimTableBody'
import {getChatroomLink, getCustomerLink, getOperatorLink} from 'utils/tabs'
import styles from './List.less'

const List = ({
                openTab, dispatch, isMotion, ...tableProps
              }) => {

  const getAvatarText = (record) => {
    const {nickName, operatorWxId} = record
    if (nickName) {
      return nickName.substr(nickName.length - 1, nickName.length)
    } else {
      return operatorWxId.substr(0, 1).toUpperCase()
    }
  }

  const columns = [
    {
      title: '头像',
      dataIndex: 'icon',
      key: 'icon',
      width: 100,
      render: (avatar, record, index) => avatar ? <Avatar src={avatar} size="large"/> :
        <Avatar style={{backgroundColor: config.randomColor(index), verticalAlign: 'middle'}}
                size="large">{getAvatarText(record)}</Avatar>,
    }, {
      title: '微信号',
      dataIndex: 'dwxId',
      key: 'dwxId',
      width: 120,
      render: (wxid, record) => getCustomerLink({
        cid: record.id,
        owxId: record.operatorWxId,
        name: record.inChatroomNickname || record.memberNickname,
        val: wxid,
      }, dispatch),
    }, {
      title: '昵称',
      dataIndex: 'memberNickname',
      key: 'nickName',
      width: 120,
    }, {
      title: '在本群的昵称',
      dataIndex: 'inChatroomNickname',
      key: 'inChatroomNickname',
      width: 120,
      render: (val, record) => val ? val : record.memberNickname,
    }, {
      title: '归属群ID',
      dataIndex: 'chatroomId',
      key: 'chatroomId',
      width: 100,
      render: (_, record) => getChatroomLink(record.chatroom, dispatch),
    }, {
      title: '归属群昵称',
      dataIndex: 'chatroomName',
      key: 'chatroomName',
      width: 100,
    }, {
      title: '归属微信',
      dataIndex: 'operatorWxId',
      key: 'operatorWxId',
      width: 100,
      render: (val, record) => getOperatorLink({wxId: record.operatorWxId, wxNo: record.operatorWxNo}, dispatch),
    }, {
      title: '录入系统时间',
      dataIndex: 'ctime',
      key: 'ctime',
      width: 120,
    }, {
      title: '性别',
      dataIndex: 'gender',
      key: 'gender',
      width: 150,
      render: gender => {
        return gender === 1 ? '男' : gender === 2 ? '女' : '未知'
      },
    }, {
      title: '省份城市',
      dataIndex: 'provinceName',
      key: 'provinceName',
      render: val => val ? val : '-',
      width: 180,
    }, {
      title: '个性签名',
      dataIndex: 'signature',
      key: 'signature',
      width: 120,
    },
  ]

  const AnimateBody = (props) => {
    return <AnimTableBody {...props} />
  }

  const CommonBody = (props) => {
    return <tbody {...props} />
  }

  return (
    <Table
      {...tableProps}
      className={classnames(styles.table)}
      scroll={{x: 1400}}
      columns={columns}
      simple
      rowKey={record => record.id}
      components={{
        body: {wrapper: isMotion ? AnimateBody : CommonBody},
      }}
    />
  )
}

List.propTypes = {
  onDeleteItem: PropTypes.func,
  onEditItem: PropTypes.func,
  location: PropTypes.object,
}

export default List
